package com.stars.services.impl.system;/**
 * Created by luodajie on 2019/1/5.
 */

import com.stars.common.base.BaseCode;
import com.stars.common.base.ResultMsg;
import com.stars.dao.mapper.system.SystemUserRelationMapper;
import com.stars.entity.sys.SystemUserRelation;
import com.stars.services.impl.BaseConfigServiceImpl;
import com.stars.services.system.UserRelationConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author :dj
 * @version : 1.0.0
 * @description
 * @Date 2019-01-05 12:33
 * @note
 */
@Service
public class UserRelationConfigServiceImpl extends BaseConfigServiceImpl<SystemUserRelationMapper, SystemUserRelation> implements UserRelationConfigService {

    @Autowired
    private SystemUserRelationMapper systemUserRelationMapper;

    @Override
    public ResultMsg userRelationInsert(SystemUserRelation command) throws Exception {
        systemUserRelationMapper.insert(command);
        return ResultMsg.success();
    }

    @Override
    public ResultMsg addUserRelation(String roleId, String[] userIds) throws Exception {
        if (null != userIds) {
            for (String userId : userIds) {
                if (StringUtils.isNotBlank(userId)) {
                    SystemUserRelation systemUserRelation = new SystemUserRelation();
                    systemUserRelation.setRelationUserId(userId);
                    systemUserRelation.setRelationUserRoleId(roleId);
                    systemUserRelationMapper.insert(systemUserRelation);
                }
            }
        }
        return ResultMsg.success();
    }

    @Override
    public ResultMsg updateEntity(SystemUserRelation systemUserRelation) throws Exception {
        SystemUserRelation byEntityId = systemUserRelationMapper.selectByPrimaryKey(systemUserRelation.getId());
        if (null == byEntityId) {
            return ResultMsg.customMsg(BaseCode.VLIDATE_PARM, "该关系不存在!", null);
        }
        SystemUserRelation byRelationIdAndRoleId =
                systemUserRelationMapper.findByRelationIdAndRoleId(systemUserRelation);
        if (null != byRelationIdAndRoleId) {
            return ResultMsg.customMsg(BaseCode.VLIDATE_PARM, "该关系已经存在，请不要重复修改!", null);
        }
        systemUserRelationMapper.updateByPrimaryKeySelective(systemUserRelation);
        return ResultMsg.success();
    }

    @Override
    public ResultMsg updateRoleAndUser(Map<String, Object> map) throws Exception {
        System.out.println(map.get("role"));

        List roles = (List) map.get("role");

        for (int i = 0; i < roles.size(); i++) {
            Map<String, Object> each = (Map<String, Object>) roles.get(i);

            String relation_user_id = each.get("relation_user_id") + "";
            String relation_user_role_id = each.get("relation_user_role_id") + "";

            SystemUserRelation sur = new SystemUserRelation();
            sur.setRelationUserId(relation_user_id);
            sur.setRelationUserRoleId(relation_user_role_id);

            if (1 == Integer.parseInt(each.get("flag") + "")) {
                systemUserRelationMapper.insertSelective(sur);
            }

            if (0 == Integer.parseInt(each.get("flag") + "")) {
                systemUserRelationMapper.deleteByRoleIdAndUserId(sur);
            }
        }
        return ResultMsg.success();
    }

    @Override
    public void delByRoleId(String roleId, String userId) throws Exception {
        if (StringUtils.isBlank(roleId)) {
            return;
        }
        if (StringUtils.isBlank(userId)) {
            return;
        }
        SystemUserRelation systemUserRelation = new SystemUserRelation();
        systemUserRelation.setRelationUserRoleId(roleId);
        systemUserRelation.setRelationUserId(userId);
        systemUserRelationMapper.deleteByRoleIdAndUserId(systemUserRelation);
    }
}
